﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Server.model
{
	/// <summary>
	/// 类account_book。
	/// </summary>
	[Serializable]
	public partial class account_book
	{
		public account_book()
		{
            this.id = -1;
            this.remark = "";
        }
		#region Model
		private int _id;
		private DateTime _o_date;
		private float _price;
		private int _type=1;
		private int _type_info;
		private int _user_id;
		private string _remark;
		/// <summary>
		/// auto_increment
		/// </summary>
		public int id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public DateTime o_date
		{
			set{ _o_date=value;}
			get{return _o_date;}
		}
		/// <summary>
		/// 
		/// </summary>
		public float price
		{
			set{ _price=value;}
			get{return _price;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int type
		{
			set{ _type=value;}
			get{return _type;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int type_info
		{
			set{ _type_info=value;}
			get{return _type_info;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int user_id
		{
			set{ _user_id=value;}
			get{return _user_id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string remark
		{
			set{ _remark=value;}
			get{return _remark;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public account_book(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`o_date`,`price`,`type`,`type_info`,`user_id`,`remark` ");
			strSql.Append(" FROM `account_book` ");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["o_date"]!=null && ds.Tables[0].Rows[0]["o_date"].ToString()!="")
				{
					this.o_date=DateTime.Parse(ds.Tables[0].Rows[0]["o_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["price"]!=null && ds.Tables[0].Rows[0]["price"].ToString()!="")
				{
					this.price=float.Parse(ds.Tables[0].Rows[0]["price"].ToString());
				}
				if(ds.Tables[0].Rows[0]["type"]!=null && ds.Tables[0].Rows[0]["type"].ToString()!="")
				{
					this.type=int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["type_info"]!=null && ds.Tables[0].Rows[0]["type_info"].ToString()!="")
				{
					this.type_info=int.Parse(ds.Tables[0].Rows[0]["type_info"].ToString());
				}
				if(ds.Tables[0].Rows[0]["user_id"]!=null && ds.Tables[0].Rows[0]["user_id"].ToString()!="")
				{
					this.user_id=int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["remark"]!=null)
				{
					this.remark=ds.Tables[0].Rows[0]["remark"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperMySQL.GetMaxID("`id`", "`account_book`"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from `account_book`");
			strSql.Append(" where `id`=@id ");

			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into `account_book` (");
			strSql.Append("`o_date`,`price`,`type`,`type_info`,`user_id`,`remark`)");
			strSql.Append(" values (");
			strSql.Append("@o_date,@price,@type,@type_info,@user_id,@remark)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@o_date", MySqlDbType.DateTime),
					new MySqlParameter("@price", MySqlDbType.Float),
					new MySqlParameter("@type", MySqlDbType.Int16,4),
					new MySqlParameter("@type_info", MySqlDbType.Int32,11),
					new MySqlParameter("@user_id", MySqlDbType.Int32,11),
					new MySqlParameter("@remark", MySqlDbType.VarChar,100)};
			parameters[0].Value = o_date;
			parameters[1].Value = price;
			parameters[2].Value = type;
			parameters[3].Value = type_info;
			parameters[4].Value = user_id;
			parameters[5].Value = remark;

			int line = DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
            if (line > 0) return true;
            else return false;
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update `account_book` set ");
			strSql.Append("`o_date`=@o_date,");
			strSql.Append("`price`=@price,");
			strSql.Append("`type`=@type,");
			strSql.Append("`type_info`=@type_info,");
			strSql.Append("`user_id`=@user_id,");
			strSql.Append("`remark`=@remark");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@o_date", MySqlDbType.DateTime),
					new MySqlParameter("@price", MySqlDbType.Float),
					new MySqlParameter("@type", MySqlDbType.Int16,4),
					new MySqlParameter("@type_info", MySqlDbType.Int32,11),
					new MySqlParameter("@user_id", MySqlDbType.Int32,11),
					new MySqlParameter("@remark", MySqlDbType.VarChar,100),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
			parameters[0].Value = o_date;
			parameters[1].Value = price;
			parameters[2].Value = type;
			parameters[3].Value = type_info;
			parameters[4].Value = user_id;
			parameters[5].Value = remark;
			parameters[6].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from `account_book` ");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`o_date`,`price`,`type`,`type_info`,`user_id,remark` ");
			strSql.Append(" FROM `account_book` ");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["o_date"]!=null && ds.Tables[0].Rows[0]["o_date"].ToString()!="")
				{
					this.o_date=DateTime.Parse(ds.Tables[0].Rows[0]["o_date"].ToString());
				}
				if(ds.Tables[0].Rows[0]["price"]!=null && ds.Tables[0].Rows[0]["price"].ToString()!="")
				{
					this.price=float.Parse(ds.Tables[0].Rows[0]["price"].ToString());
				}
				if(ds.Tables[0].Rows[0]["type"]!=null && ds.Tables[0].Rows[0]["type"].ToString()!="")
				{
					this.type=int.Parse(ds.Tables[0].Rows[0]["type"].ToString());
				}
				if(ds.Tables[0].Rows[0]["type_info"]!=null && ds.Tables[0].Rows[0]["type_info"].ToString()!="")
				{
					this.type_info=int.Parse(ds.Tables[0].Rows[0]["type_info"].ToString());
				}
				if(ds.Tables[0].Rows[0]["user_id"]!=null && ds.Tables[0].Rows[0]["user_id"].ToString()!="")
				{
					this.user_id=int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["remark"]!=null )
				{
					this.remark=ds.Tables[0].Rows[0]["remark"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM `account_book` ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere); 
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

